home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2009 February / PCWFEB09.iso / Software / Linux / SLAX 6.0.8 / slax-6.0.8.iso / slax / base / 006-devel.lzm / usr / include / kunittest / runner.h < prev    next >
Encoding:
C/C++ Source or Header  |  2005-11-08  |  8.2 KB  |  220 lines

  1. /*
  2.  * kunittest.h
  3.  *
  4.  * Copyright (C)  2004  Zack Rusin <zack@kde.org>
  5.  *
  6.  * Redistribution and use in source and binary forms, with or without
  7.  * modification, are permitted provided that the following conditions
  8.  * are met:
  9.  *
  10.  * 1. Redistributions of source code must retain the above copyright
  11.  *   notice, this list of conditions and the following disclaimer.
  12.  * 2. Redistributions in binary form must reproduce the above copyright
  13.  *   notice, this list of conditions and the following disclaimer in the
  14.  *   documentation and/or other materials provided with the distribution.
  15.  *
  16.  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  17.  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  18.  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  19.  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  20.  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  21.  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  22.  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  23.  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24.  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  25.  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26.  */
  27.  
  28. /*!
  29.  * @file runner.h
  30.  * Defines a set of macros and classes for running unit tests
  31.  */
  32.  
  33. #ifndef KUNITTEST_RUNNER_H
  34. #define KUNITTEST_RUNNER_H
  35.  
  36. #include <iostream>
  37. using namespace std;
  38.  
  39. #include <qobject.h>
  40. #include <qasciidict.h>
  41. #include <qstring.h>
  42.  
  43. #include <kdelibs_export.h>
  44.  
  45. #include "tester.h"
  46.  
  47. class QSocketNotifier;
  48.  
  49. namespace KUnitTest
  50. {
  51.     /*! @def KUNITTEST_SUITE(suite)
  52.      * 
  53.      * This macro must be used if you are not making a test-module. The macro
  54.      * defines the name of the test suite.
  55.      */
  56.     #define KUNITTEST_SUITE(suite)\
  57.     static const QString s_kunittest_suite  = suite;
  58.  
  59.     /*! @def KUNITTEST_REGISTER_TESTER( tester )
  60.      * @brief Automatic registration of Tester classes.
  61.      *
  62.      * This macro can be used to register the Tester into the global registry. Use
  63.      * this macro in the implementation file of your Tester class. If you keep the
  64.      * Tester classes in a shared or convenience library then you should not use this
  65.      * macro as this macro relies on the static initialization of a TesterAutoregister class.
  66.      * You can always use the static Runner::registerTester(const char *name, Tester *test) method.
  67.     */
  68.     #define KUNITTEST_REGISTER_TESTER( tester )\
  69.     static TesterAutoregister tester##Autoregister( QString(s_kunittest_suite + QString("::") + QString::fromLocal8Bit(#tester)).local8Bit() , new tester ())
  70.  
  71.     #define KUNITTEST_REGISTER_NAMEDTESTER( name, tester )\
  72.     static TesterAutoregister tester##Autoregister( QString(s_kunittest_suite + QString("::") + QString::fromLocal8Bit(name)).local8Bit() , new tester ())
  73.  
  74.     /*! The type of the registry. */
  75.     typedef QAsciiDict<Tester> RegistryType;
  76.  
  77.     /*! A type that can be used to iterate through the registry. */
  78.     typedef QAsciiDictIterator<Tester> RegistryIteratorType;
  79.     
  80.     /*! The Runner class holds a list of registered Tester classes and is able
  81.      * to run those test cases. The Runner class follows the singleton design
  82.      * pattern, which means that you can only have one Runner instance. This
  83.      * instance can be retrieved using the Runner::self() method.
  84.      *
  85.      * The registry is an object of type RegistryType, it is able to map the name
  86.      * of a test to a pointer to a Tester object. The registry is also a singleton
  87.      * and can be accessed via Runner::registry(). Since there is only one registry,
  88.      * which can be accessed at all times, test cases can be added without having to
  89.      * worry if a Runner instance is present or not. This allows for a design in which
  90.      * the KUnitTest library can be kept separate from the test case sources. Test cases
  91.      * (classes inheriting from Tester) can be added using the static 
  92.      * registerTester(const char *name, Tester *test) method. Allthough most users
  93.      * will want to use the KUNITTEST_REGISTER_TESTER macro.
  94.      *
  95.      * @see KUNITTEST_REGISTER_TESTER
  96.      */
  97.     class KUNITTEST_EXPORT Runner : public QObject
  98.     {
  99.         Q_OBJECT
  100.     
  101.     public:
  102.         /*! Registers a test case. A registry will be automatically created if necessary.
  103.          * @param name The name of the test case.
  104.          * @param test A pointer to a Tester object.
  105.          */
  106.         static void registerTester(const char *name, Tester *test);
  107.  
  108.         /*! @returns The registry holding all the Tester objects.
  109.          */
  110.         RegistryType ®istry();
  111.  
  112.         /*! @returns The global Runner instance. If necessary an instance will be created.
  113.          */
  114.         static Runner *self();
  115.  
  116.         /*! @returns The number of registered test cases.
  117.          */
  118.         int numberOfTestCases();
  119.  
  120.         /*! Load all modules found in the folder. 
  121.          * @param folder The folder where to look for modules.
  122.          * @param query A regular expression. Only modules which match the query will be run.
  123.          */
  124.         static void loadModules(const QString &folder, const QString &query);
  125.  
  126.         /*! The runner can spit out special debug messages needed by the Perl script: kunittest_debughelper.
  127.          * This script can attach the debug output of each suite to the results in the KUnitTest GUI.
  128.          * Not very useful for console minded developers, so this static method can be used to disable
  129.          * those debug messages.
  130.          * @param enabled If true the debug messages are enabled (default), otherwise they are disabled.
  131.          */
  132.         static void setDebugCapturingEnabled(bool enabled);
  133.             
  134.     private:
  135.         RegistryType         m_registry;
  136.         static Runner       *s_self;
  137.         static bool          s_debugCapturingEnabled;
  138.     
  139.     protected:
  140.         Runner();
  141.     
  142.     public:
  143.         /*! @returns The number of finished tests. */
  144.         int numberOfTests() const;
  145.  
  146.         /*! @returns The number of passed tests. */
  147.         int numberOfPassedTests() const;
  148.  
  149.         /*! @returns The number of failed tests, this includes the number of expected failures. */
  150.         int numberOfFailedTests() const;
  151.  
  152.         /*! @returns The number of failed tests which were expected. */
  153.         int numberOfExpectedFailures() const;
  154.  
  155.         /*! @returns The number of skipped tests. */
  156.         int numberOfSkippedTests() const;
  157.  
  158.     public slots:
  159.         /*! Call this slot to run all the registered tests.
  160.          * @returns The number of finished tests.
  161.          */
  162.         int runTests();
  163.  
  164.         /*! Call this slot to run a single test.
  165.          * @param name The name of the test case. This name has to correspond to the name
  166.          * that was used to register the test. If the KUNITTEST_REGISTER_TESTER macro was
  167.          * used to register the test case then this name is the class name.
  168.          */
  169.         void runTest(const char *name);
  170.  
  171.         /*! Call this slot to run tests with names starting with prefix.
  172.          * @param prefix Only run tests starting with the string prefix.
  173.          */
  174.         void runMatchingTests(const QString &prefix);
  175.  
  176.         /*! Reset the Runner in order to prepare it to run one or more tests again.
  177.          */
  178.         void reset();
  179.  
  180.     signals:
  181.         /*! Emitted after a test is finished.
  182.          * @param name The name of the test.
  183.          * @param test A pointer to the Tester object.
  184.          */
  185.         void finished(const char *name, Tester *test);
  186.         void invoke();
  187.     
  188.     private:
  189.         void registerTests();
  190.     
  191.     private:
  192.         int globalSteps;
  193.         int globalTests;
  194.         int globalPasses;
  195.         int globalFails;
  196.         int globalXFails;
  197.         int globalXPasses;
  198.         int globalSkipped;
  199.     };
  200.     
  201.     /*! The TesterAutoregister is a helper class to allow the automatic registration
  202.      * of Tester classes.
  203.      */
  204.     class TesterAutoregister
  205.     {
  206.     public:
  207.         /*! @param name A unique name that identifies the Tester class.
  208.          * @param test A pointer to a Tester object.
  209.          */
  210.         TesterAutoregister(const char *name, Tester *test) 
  211.         { 
  212.             if ( test->name() == 0L ) test->setName(name);
  213.             Runner::registerTester(name, test); 
  214.         }
  215.     };
  216.  
  217. }
  218.  
  219. #endif
  220.